package com.taskmanager.controller;

import com.taskmanager.dto.CategoryRequest;
import com.taskmanager.dto.CategoryResponse;
import com.taskmanager.service.CategoryService;
import com.taskmanager.util.ApiResponse;
import com.taskmanager.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 分类控制器
 * 处理分类的增删改查请求
 */
@RestController
@RequestMapping("/api/category")
public class CategoryController {
    
    @Autowired
    private CategoryService categoryService;
    
    /**
     * 查询分类列表
     * GET /api/category/list
     */
    @GetMapping("/list")
    public ApiResponse<List<CategoryResponse>> getCategoryList(HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            List<CategoryResponse> categories = categoryService.getCategoriesByUserId(userId);
            return ResponseUtil.success(categories);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.serverError("查询分类列表失败");
        }
    }
    
    /**
     * 创建分类
     * POST /api/category
     */
    @PostMapping
    public ApiResponse<CategoryResponse> createCategory(
            @Validated @RequestBody CategoryRequest request,
            HttpServletRequest httpRequest) {
        try {
            Long userId = (Long) httpRequest.getAttribute("userId");
            CategoryResponse response = categoryService.createCategory(userId, request);
            return ResponseUtil.success("创建成功", response);
        } catch (IllegalArgumentException e) {
            return ResponseUtil.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.serverError("创建分类失败");
        }
    }
    
    /**
     * 更新分类
     * PUT /api/category/{categoryId}
     */
    @PutMapping("/{categoryId}")
    public ApiResponse<CategoryResponse> updateCategory(
            @PathVariable Long categoryId,
            @Validated @RequestBody CategoryRequest request,
            HttpServletRequest httpRequest) {
        try {
            Long userId = (Long) httpRequest.getAttribute("userId");
            CategoryResponse response = categoryService.updateCategory(userId, categoryId, request);
            return ResponseUtil.success("更新成功", response);
        } catch (IllegalArgumentException e) {
            return ResponseUtil.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.serverError("更新分类失败");
        }
    }
    
    /**
     * 删除分类
     * DELETE /api/category/{categoryId}
     */
    @DeleteMapping("/{categoryId}")
    public ApiResponse<Void> deleteCategory(
            @PathVariable Long categoryId,
            HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            categoryService.deleteCategory(userId, categoryId);
            return ResponseUtil.success("删除成功，该分类下的任务已移至'其他'分类");
        } catch (IllegalArgumentException e) {
            return ResponseUtil.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.serverError("删除分类失败");
        }
    }
}
